#include "../include/led.h"
#include "../include/stm32mp157_rcc.h"
#include "../include/stm32mp157_gpio.h"
#include "../include/uart.h"
void led_init(void)
{
    //GPIOE时钟使能
    RCC->MP_AHB4ENSETR |= (1UL << 4);//GPIOE
    RCC->MP_AHB4ENSETR |= (1UL << 5);//
    //输出模式
    GPIOE->MODER &= ~(3UL << 20);
    GPIOE->MODER |= (1UL << 20);//PE10 LED1
    GPIOF->MODER &= ~(3UL << 20);
    GPIOF->MODER |= (1UL << 20);//PF10 LED2
    GPIOE->MODER &= ~(3UL << 16);
    GPIOE->MODER |= (1UL << 16);//PE8 LED3
    //推挽输出
    GPIOE->OTYPER &= ~(1UL << 10);//PE10
    GPIOF->OTYPER &= ~(1UL << 10);//PF10
    GPIOE->OTYPER &= ~(1UL << 8);//PE8
    //设置PE10,PF10引脚为High speed
    GPIOE->OSPEEDR &= ~(3UL << 20);
    GPIOE->OSPEEDR |= (2UL << 20);//PE10
    GPIOF->OSPEEDR &= ~(3UL << 20);
    GPIOF->OSPEEDR |= (2UL << 20);//PF10
    GPIOE->OSPEEDR &= ~(3UL << 16);
    GPIOE->OSPEEDR |= (2UL << 16);//PE8
    //设置无上下拉
    GPIOE->PUPDR &= ~(3UL << 20);//PE10
    GPIOF->PUPDR &= ~(3UL << 20);//PF10
    GPIOE->PUPDR &= ~(3UL << 16);//PE8
}

void led_operator(short location, short state)
{
    switch(location)
    {
        case 1:
        {
            if(state)
                GPIOE->ODR |= (1UL << 10);
            else
                GPIOE->ODR &= ~(1UL << 10);
            break;
        }
        case 2:
        {
            if (state)
                GPIOF->ODR |= (1UL << 10);
            else
                GPIOF->ODR &= ~(1UL << 10);
                break;
        }
        case 3:
        {
            if(state)
                GPIOE->ODR |= (1UL << 8);
            else
                GPIOE->ODR &= ~(1UL << 8);
            break;
        }
        default:break;
    }
}

void led1_on(void){led_operator(1,1);}
void led1_off(void){led_operator(1,0);}
void led2_on(void){led_operator(2,1);}
void led2_off(void){led_operator(2,0);}
void led3_on(void){led_operator(3,1);}
void led3_off(void){led_operator(3,0);}